package com.xfn.redis.impl;

import com.xfn.redis.RedisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class RedisImpl implements RedisMapper {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisMapper redisMapper;

    @Override
    public Map<String, String> redisTest() {
        String key = "kk";
        ValueOperations<String,Map<String,String>> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(key);
        if(hasKey){
            Map<String,String> mapa = operations.get(key);
            System.out.println("redis缓存："+ mapa.toString());
            return mapa;
        }else {
            Map<String,String> map = redisMapper.redisTest();
            System.out.println("数据库："+map.toString());
            operations.set(key,map,5, TimeUnit.MINUTES);
            return map;

        }

    }
}
